

//
//  CollectViewController.m
//  nncloud
//
//  Created by macfeel on 2019/7/11.
//  Copyright © 2019 macfai. All rights reserved.
//

#import "CollectViewController.h"
#import "UICustomDatePicker.h"
#import "FillViewController.h"

@interface CollectViewController ()
<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)NSArray *titleArray;
@property (nonatomic,strong)NSArray *detailArray;
@property (nonatomic,assign)NSInteger currentRow;

///标记日期
@property(nonatomic,assign)BOOL isDate;

@property(nonatomic,copy)NSString *dateString;
@property(nonatomic,copy)NSString *content;

@end

@implementation CollectViewController

#define cellIdentifier  @"cellId"

//-(void)viewWillAppear:(BOOL)animated{
//
//    [super viewWillAppear:animated];
//    self.navigationController.navigationBar.hidden = false;
//}
//
//-(void)viewWillDisappear:(BOOL)animated{
//
//    [super viewWillDisappear:animated];
//    self.navigationController.navigationBar.hidden = YES;
//
//}

-(UITableView *)tableView{
    
    if (!_tableView) {
        //        WeakStament(ws);
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - NavBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 50.f;
        //        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = kBackgroundColor;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setItemWithTitle:@"设备采集" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    self.currentRow = -1;
    [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 35, 35) title:@"确认" Image:@"" selectedIMG:@"" tartget:self action:@selector(sureAction)];
    self.titleArray = @[@"设备编号",@"型号",@"站名",@"位置",@"出厂日期",@"生产厂家",@"出所日期",@"到期日期",@"周期",];
    self.detailArray = @[@"",@"",@"",@"",@"",@"",@"",@"",@"",];
    
    [self.view addSubview:self.tableView];
    
    self.isDate = false;
    
    // Do any additional setup after loading the view.
}

-(void)sureAction{
    
    YYLog(@"确认修改");
    [self.navigationController popViewControllerAnimated:YES];
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ///detail 不显示
    //    NSDictionary *dict = self.dict;
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1
                                      reuseIdentifier: cellIdentifier];
    }
    cell.selectionStyle = 0;
    cell.textLabel.textColor = kDarkGrayColor;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = self.titleArray[indexPath.row];
    cell.detailTextLabel.text = self.detailArray[indexPath.row];
    
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.currentRow = indexPath.row;
    
    YYLog(@"%li",(long)self.currentRow);
    
    NSString *titleString = self.titleArray[indexPath.row];
    
    if ([titleString containsString:@"日期"]) {
        
        [UICustomDatePicker showCustomDatePickerAtView:self.view choosedDateBlock:^(NSDate *date) {
            NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
            fmt.dateFormat = @"yyyy-MM-dd";
            NSString *string = [fmt stringFromDate:date];
            YYLog(@"current Date:%@",string);
            ///将选择的时间放到数据源中
            self.dateString = string;
            self.isDate = YES;
            [self updateDateToDataSource];
            
        } cancelBlock:^{
            
        }];
        
    }else{
        ///填写
        FillViewController *fill = [FillViewController new];
        fill.block = ^(NSString * _Nonnull content) {
            YYLog(@"%@",content);
            self.content = content;
            [self updateDataSource];
        };
        [self.navigationController pushViewController:fill animated:YES];
    }
    
}

-(void)updateDateToDataSource{
    [self updateDataSource];
    self.isDate = false;
}
///更新数据源
-(void)updateDataSource{
    
    NSMutableArray *newArray = [self.detailArray mutableCopy];
    
    if (self.isDate==YES) {
        ///选择日期
        [newArray replaceObjectAtIndex:self.currentRow withObject:self.dateString?:@""];
        
    }else{
        [newArray replaceObjectAtIndex:self.currentRow withObject:self.content?:@""];
    }
    
    self.detailArray = newArray;
    [self.tableView reloadData];
    
    ///这里还可以加上刷新某一行
}


@end
